package com.tm.action;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.tm.util.SessionManager;
import com.util.CommonResultMap;
import com.util.exception.ActionExceptioon;

public abstract class DefaultAction extends ActionSupport implements SessionAware,RequestAware,ApplicationAware{
	
	private static final long serialVersionUID = 4601368362039733567L;
	public static final String SESSION_MANAGER = "SM";
	public static final String LOGIN = "LOGIN"; 
	public static final String HOME =  "HOME";
	public static final String COMMON_MSG = "COMMON_MSG";
	
	protected Map<String,Object> request;
	protected Map<String,Object> session;
	protected Map<String,Object> application;
	

	@Override
	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}
	
	protected String  addActionError(CommonResultMap retEnum){
			String message = getText(retEnum.getMessage());
			if(StringUtils.isBlank(message)){
				message = "ERROR "+retEnum.getMessage();
			}
			addActionError(message);
			return COMMON_MSG;
	}
	
	protected String setCommonMessage(String message){
		this.addActionMessage(message);
		return COMMON_MSG;
	}
	
	public SessionManager getSessionManager(){
		SessionManager m = (SessionManager)session.get(DefaultAction.SESSION_MANAGER);
		if(m == null){
			m = new SessionManager();
			session.put(DefaultAction.SESSION_MANAGER, m);
		}
		return m;
	}
	
	@Override
	public abstract String execute() throws ActionExceptioon;
	
}
